/***********************************************************************************************/
/***********************************************************************************************/
//
#ifndef LOAD_SENSING                                                       //
#define LOAD_SENSING                                                       //

#include "fet.h"
//
int first_order_low_pass_filter(long *l_Old_Filtered_Value                              //
                                , int ui_Time_Constant                                //
                                , int ui_New_Measurement);                            //
//
int load_sense(unsigned int   ui_Calibrated_Scissor_Angle_Array[]                        // Input: The array of stored scissor angles
               , unsigned int  ui_Calibrated_Cylinder_Pressure_Array[]                    // Input: The cooresponding array of stored cylinder pressures
               , unsigned int  ui_NoLoad_Angle_Sensor_Array[]                          // Input: The array of stored scissor angles
               , unsigned int  ui_NoLoad_Pressure_Sensor_Array[]                       // Input: The cooresponding array of stored cylinder pressures
               , unsigned int  ui_Up_Calibrated_Scissor_Angle_Array[]                  // Input: The array of stored scissor angles
               , unsigned int  ui_Up_Calibrated_Cylinder_Pressure_Array[]                 // Input: The cooresponding array of stored cylinder pressures
               , unsigned int  ui_Up_NoLoad_Angle_Sensor_Array[]                       // Input: The array of stored scissor angles
               , unsigned int  ui_Up_NoLoad_Pressure_Sensor_Array[]                    // Input: The cooresponding array of stored cylinder pressures
               , unsigned int  ui_Dn_Calibrated_Scissor_Angle_Array[]                  // Input: The array of stored scissor angles
               , unsigned int  ui_Dn_Calibrated_Cylinder_Pressure_Array[]                 // Input: The cooresponding array of stored cylinder pressures
               , unsigned int  ui_Dn_NoLoad_Angle_Sensor_Array[]                       // Input: The array of stored scissor angles
               , unsigned int  ui_Dn_NoLoad_Pressure_Sensor_Array[]                    // Input: The cooresponding array of stored cylinder pressures
               , unsigned char uc_Load_Sensing_Array_Size                           // Input: The number of elements in each stored array
               , unsigned int  *ui_Measured_Scissor_Angle                           // Output: The measured scissor angle as measure in this function
               , unsigned int  *ui_Measured_Cylinder_Pressure                       // Output: The measured cylinder pressure as measured in this function
               , unsigned int  *ui_Measured_Cylinder_Pressure2);                                                           //
unsigned char max_angle(unsigned int   ui_Loaded_Calibrated_Angle_Array[]                  // Output: The array of stored scissor angles
                        , unsigned int  ui_Unloaded_Angle_Sensor_Array[]               // Output: The array of stored scissor angles
                        , unsigned char uc_Load_Sensing_Array_Size                     // Input: The number of elements in each stored array
                        , unsigned int  *ui_Max_Angle);                                // Input: The number of elements in each stored array
//
unsigned int interpolate_curve(unsigned int ui_Array1[]
                               , unsigned int ui_Array2[]
                               , unsigned char uc_array_size
                               , unsigned int *Array1_value
                               , unsigned int *Array2_value);

unsigned int read_angle_sensor(void);                                            //
unsigned int read_cylinder_pressure(void);                                          //
unsigned int read_cylinder_pressure2(void);                                                                 //
#define ANGLE_SENSOR_AD_CHANNEL  2                                            //
#define ANGLE_SENSOR_TIME_CONSTANT 100                                           // max 2000 mSec and must be in even multiples of the background loop (10 mSec)
#define BACKGROUND_LOOP_TIME 10                                                  // 10 mSec.
#define CYLINDER_PRESSURE_TIME_CONSTANT 100                                         // max 2000 mSec and must be in even multiples of the background loop (10 mSec)
//#define DUAL_CALIBRATION 1//lift_options.fOverload2_cutout                              // Set to 1 only if using both an empty platform calibration and a fully loaded platform calibration.
#define LOAD_SENSING_ARRAY_SIZE  30                                              // The maximum is 60. (Limited by the EEPROM block size).
#define TIME_ARRAY_SIZE    4                                                     // The maximum is 60. (Limited by the EEPROM block size).
#define PRESSURE_SENSOR_AD_CHANNEL_1   0                                         //
#define PRESSURE_SENSOR_AD_CHANNEL_2   1                                         //
#define PRESSURE_DIFFERENCE_THRESHOLD  60 //30                                      // 3% of full scale for 10-bit A/D conversion. (For a 2000 lb. transducer, this is about 59 lbs)
#define PRESSURE_SENSING_TIME_CONSTANT 300                                          // max 2000 mSec and must be in even multiples of the background loop (10 mSec)
#define UPTIME_CONSTANT 10l
#define CAL_OFFSET      400
#define Iv_Switch_Voltage_ON {vSetStatusIV1(0);vSetStatusIV2(0);}                                                                       //
#define Iv_Switch_Current_ON {vSetStatusIV1(1);vSetStatusIV2(1);}
//extern void delay_mSec (unsigned int ui_Delay);                                      //
extern Fet_t Fets;                                                            //
//
#endif                                                                     //
//
/******END**********END**********END**********END**********END**********END**********END********/
